package com.ecommerce.product.converter;

import com.ecommerce.product.domain.Product;
import com.ecommerce.product.domain.ProductDocument;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

/**
 * 使用mapstruct，将Product 类型的对象转换为 其它 类型的对象
 */
@Mapper(componentModel = "spring",// 关键注解，这会让 MapStruct 生成 Spring 可管理的 bean。
        uses = {ProductJsonConverter.class}, // 自定义转换器，ProductJsonConverter 作为专用转换器，尽量不把转换方法写在当前接口中（Java8接口中可以写default方法，但当前接口中尽量不要写），简化Mapper接口，符合单一职责原则。
        unmappedTargetPolicy = ReportingPolicy.IGNORE,
        unmappedSourcePolicy = ReportingPolicy.IGNORE
)
public interface ProductConverterMapper {
    ProductConverterMapper INSTANCE = Mappers.getMapper(ProductConverterMapper.class);

    /**
     * Product 类型的对象转换为 ProductDocument 类型的对象。
     * 此方法只做了Product基础字段映射，只映射能自动转换的基础字段，后续还要手动补充 MapStruct 无法处理的字段
     */
    @Mapping(target = "id", source = "id")
    @Mapping(target = "name", source = "name")
    @Mapping(target = "code", source = "code")
    @Mapping(target = "stock", source = "stock")
    @Mapping(target = "productType", source = "productType")
    @Mapping(target = "createTime", source = "createTime")
    @Mapping(target = "updateTime", source = "updateTime")
    @Mapping(target = "specs", ignore = true) // 忽略复杂字段
    @Mapping(target = "categoryName", ignore = true)
    @Mapping(target = "subCategoryName", ignore = true)
    @Mapping(target = "price", ignore = true)
    ProductDocument toProductDocumentBase(Product product);
}